﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using VIT.DataHelper.DataAccess;

namespace VIT.Business
{
    public class Folder : Folder_Base
    {
        SQLSupport sqlSupport = new SQLSupport();

        #region Constructor
        public Folder(string Id = "",
                        string ParentId = "",
                        string LanguageId = "",
                        string Name = "",
                        string Description = "")
        {
            this.Id = Id;
            this.ParentId = ParentId;
            this.LanguageId = LanguageId;
            this.Name = Name;
            this.Description = Description;
        }
        #endregion

        #region FrontEnd
        public DataTable GetList(string folderId="")
        {
            string filter = folderId == "" ? "" : " and FolderId = @FolderId";
            string strQuery = "select Id, Name, ParentId, Description from Folder, Folder_Language where Id=FolderId and LanguageID=@LanguageID" 
                    + filter + " and Published=1 order by Orders";
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            SqlParameter FolderId = new SqlParameter("@FolderId", folderId);
            return sqlSupport.ExecuteDataset(false, strQuery, LanguageId, FolderId).Tables[0];
        }

        public void GetSingle()
        {            
            string strQuery = "select ParentId, Name, Description from Folder, Folder_Language" +
                    " where Id=FolderId and LanguageID=@LanguageId and Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            DataTable dt = sqlSupport.ExecuteDataset(false, strQuery, Id, LanguageId).Tables[0];

            if (dt.Rows.Count > 0)
            {                
                this.Name = dt.Rows[0]["Name"].ToString();
                this.Description = dt.Rows[0]["Description"].ToString();
                this.ParentId = dt.Rows[0]["ParentId"].ToString();                
            }
            else this.Id = "";
        }        
        #endregion        
    }
}
